package com.example.telephonelib;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.telephony.CellIdentityCdma;
import android.telephony.CellIdentityLte;
import android.telephony.CellIdentityNr;
import android.telephony.CellIdentityTdscdma;
import android.telephony.CellIdentityWcdma;
import android.telephony.CellInfo;
import android.telephony.CellInfoCdma;
import android.telephony.CellInfoGsm;
import android.telephony.CellInfoLte;
import android.telephony.CellInfoNr;
import android.telephony.CellInfoTdscdma;
import android.telephony.CellInfoWcdma;
import android.telephony.CellSignalStrengthCdma;
import android.telephony.CellSignalStrengthGsm;
import android.telephony.CellSignalStrengthLte;
import android.telephony.CellSignalStrengthNr;
import android.telephony.CellSignalStrengthTdscdma;
import android.telephony.CellSignalStrengthWcdma;
import android.telephony.TelephonyManager;
import android.util.Log;

import androidx.core.app.ActivityCompat;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import io.dcloud.feature.uniapp.annotation.UniJSMethod;
import io.dcloud.feature.uniapp.bridge.UniJSCallback;
import io.dcloud.feature.uniapp.common.UniModule;

public class TestModule extends UniModule {

    private static final String TAG = "TestModule";
    private static final int PERMISSION_REQUEST_COARSE_LOCATION = 1;

    @UniJSMethod(uiThread = true)
    public void getCellInfoData(UniJSCallback callback) {
        this.checkPermission();
        JSONArray list = getCellInfoList();
        JSONObject result = new JSONObject();

        System.out.println("内部打印" + list);
        System.out.println("内部打印---" + list.size());
        if (list.size() > 0) {
            result.put("status", "200");
            result.put("message", "获取成功");
            result.put("data", list);
            System.out.println("返回参数" + result);
            callback.invoke(result);
        } else {
            result.put("status", "500");
            result.put("message", "获取失败");
            result.put("data", list);
            System.out.println("返回参数" + result);
            callback.invoke(result);
        }
    }


    private JSONArray getCellInfoList() {
        JSONArray list = new JSONArray();
        if (ActivityCompat.checkSelfPermission(this.mUniSDKInstance.getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            Log.w(TAG, "没有权限");
            return list;
        }
        TelephonyManager telephonyManager = (TelephonyManager) this.mUniSDKInstance.getContext().getSystemService(Context.TELEPHONY_SERVICE);
        if (telephonyManager == null) {
            Log.w(TAG, "telephonyManager is null");
            return list;
        }
        if (telephonyManager.getAllCellInfo() == null || telephonyManager.getAllCellInfo().size() == 0) {
            Log.w(TAG, "未查询到基站信息");
            return list;
        }
        for (int i = 0; i < telephonyManager.getAllCellInfo().size(); i++) {
            CellInfo cellInfo = telephonyManager.getAllCellInfo().get(i);
            Log.w(TAG, "cellInfo: " + cellInfo);
            //cellinfo Log参数为cellInfo: CellInfoNr:{ mRegistered=YES mTimeStamp=3711769268512208ns mCellConnectionStatus=0 CellIdentityNr:{ mMcc = 460 mMnc = 01 mAlphaLong = CHN-UNICOM mAlphaShort = UNICOM mAdditionalPlmns = {} } CellSignalStrengthNr:{ csiRsrp = 2147483647 csiRsrq = 2147483647 csiCqiTableIndex = 2147483647 csiCqiReport = [] ssRsrp = -101 ssRsrq = -12 ssSinr = 6 miuiLevel = 0 mOptimizedLevel = 0 level = 1 parametersUseForLevel = 0 timingAdvance = 2147483647 } }

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                if (cellInfo instanceof CellInfoNr) {
                    CellInfoNr cellInfoNr = (CellInfoNr) cellInfo;
                    CellIdentityNr cellIdentityNr = (CellIdentityNr) cellInfoNr.getCellIdentity();
                    CellSignalStrengthNr signalStrengthNr = (CellSignalStrengthNr) cellInfoNr.getCellSignalStrength();
                    String mcc = cellIdentityNr.getMccString();
                    String mnc = cellIdentityNr.getMncString();
                    int pci = cellIdentityNr.getPci();
                    int tac = cellIdentityNr.getTac();
                    String nci = String.valueOf(cellIdentityNr.getNci());
                    int nrarfcn = cellIdentityNr.getNrarfcn();
                    int irsrp = signalStrengthNr.getCsiRsrp();
                    int irsrq = signalStrengthNr.getCsiRsrq();
                    int sinr = signalStrengthNr.getCsiSinr();
                    int rsrp = signalStrengthNr.getSsRsrp();
                    int rsrq = signalStrengthNr.getSsRsrq();
                    int isinr = signalStrengthNr.getSsSinr();
                    // 处理连接的5G NR基站信息
                    JSONObject obj = new JSONObject();
                    if (i == 0) {
                        obj.put("type", "Connected");
                    } else {
                        obj.put("type", "Neighboring");
                    }
                    obj.put("type1", "5G NR");
                    obj.put("mcc", mcc);
                    obj.put("mnc", mnc);
                    obj.put("pci", pci);
                    obj.put("tac", tac);
                    obj.put("nci", nci);
                    obj.put("nrarfcn", nrarfcn);
                    obj.put("rsrp", rsrp);
                    obj.put("rsrq", rsrq);
                    obj.put("sinr", sinr);
                    obj.put("irsrp", irsrp);
                    obj.put("irsrq", irsrq);
                    obj.put("isinr", isinr);
                    list.add(obj);
                } else if (cellInfo instanceof CellInfoTdscdma) {
                    CellInfoTdscdma cellInfoTdscdma = (CellInfoTdscdma) cellInfo;
                    CellIdentityTdscdma cellIdentityTdscdma = cellInfoTdscdma.getCellIdentity();
                    CellSignalStrengthTdscdma signalStrengthTdscdma = cellInfoTdscdma.getCellSignalStrength();
                    String mcc = cellIdentityTdscdma.getMccString();
                    String mnc = cellIdentityTdscdma.getMncString();
                    int cid = cellIdentityTdscdma.getCid();
                    int lac = cellIdentityTdscdma.getLac();
                    int cpid = cellIdentityTdscdma.getCpid();
                    int uarfcn = cellIdentityTdscdma.getUarfcn();
                    int rsrp = signalStrengthTdscdma.getRscp();
                    int level = signalStrengthTdscdma.getLevel();
                    int dbm = signalStrengthTdscdma.getDbm();
                    // 处理连接的TD-SCDMA基站信息
                    JSONObject obj = new JSONObject();
                    if (i == 0) {
                        obj.put("type", "Connected");
                    } else {
                        obj.put("type", "Neighboring");
                    }
                    obj.put("type1", "TD-SCDMA");
                    obj.put("mcc", mcc);
                    obj.put("mnc", mnc);
                    obj.put("cid", cid);
                    obj.put("cpid", cpid);
                    obj.put("uarfcn", uarfcn);
                    obj.put("lac", lac);
                    obj.put("rsrp", rsrp);
                    obj.put("level", level);
                    obj.put("rssi", dbm);
                    list.add(obj);
                }
            } else {
                if (cellInfo instanceof CellInfoGsm) {
                    CellInfoGsm cellInfoGsm = (CellInfoGsm) cellInfo;
                    CellSignalStrengthGsm signalStrengthGsm = cellInfoGsm.getCellSignalStrength();
                    int mcc = cellInfoGsm.getCellIdentity().getMcc();
                    int mnc = cellInfoGsm.getCellIdentity().getMnc();
                    int lac = cellInfoGsm.getCellIdentity().getLac();
                    int cid = cellInfoGsm.getCellIdentity().getCid();
                    int signalStrength = signalStrengthGsm.getDbm();
                    // 处理连接的GSM基站信息
                    JSONObject obj = new JSONObject();
                    if (i == 0) {
                        obj.put("type", "Connected");
                    } else {
                        obj.put("type", "Neighboring");
                    }
                    obj.put("type1", "GSM");
                    obj.put("mcc", mcc);
                    obj.put("mnc", mnc);
                    obj.put("cid", cid);
                    obj.put("lac", lac);
                    obj.put("rssi", signalStrength);
                    list.add(obj);
                } else if (cellInfo instanceof CellInfoCdma) {
                    CellInfoCdma cellInfoCdma = (CellInfoCdma) cellInfo;
                    CellSignalStrengthCdma signalStrengthCdma = cellInfoCdma.getCellSignalStrength();
                    String mcc = "";
                    int mnc = cellInfoCdma.getCellIdentity().getNetworkId() % 1000;
                    int sid = cellInfoCdma.getCellIdentity().getSystemId();
                    int bsic = cellInfoCdma.getCellIdentity().getBasestationId();
                    int signalStrength = signalStrengthCdma.getDbm();
                    // 处理连接的CDMA基站信息
                    JSONObject obj = new JSONObject();
                    if (i == 0) {
                        obj.put("type", "Connected");
                    } else {
                        obj.put("type", "Neighboring");
                    }
                    obj.put("type1", "CDMA");
                    obj.put("mcc", mcc);
                    obj.put("mnc", mnc);
                    obj.put("sid", sid);
                    obj.put("bsic", bsic);
                    obj.put("rssi", signalStrength);
                    list.add(obj);
                } else if (cellInfo instanceof CellInfoLte) {
                    CellInfoLte cellInfoLte = (CellInfoLte) cellInfo;
                    CellIdentityLte cellIdentityLte = cellInfoLte.getCellIdentity();
                    CellSignalStrengthLte signalStrengthLte = cellInfoLte.getCellSignalStrength();
                    int mcc = cellIdentityLte.getMcc();
                    int mnc = cellIdentityLte.getMnc();
                    int cid = cellIdentityLte.getCi();
                    int lac = cellIdentityLte.getTac();
                    int signalStrength = signalStrengthLte.getDbm();
                    // 处理连接的LTE基站信息
                    JSONObject obj = new JSONObject();
                    if (i == 0) {
                        obj.put("type", "Connected");
                    } else {
                        obj.put("type", "Neighboring");
                    }

                    obj.put("type1", "LTE");
                    obj.put("mcc", mcc);
                    obj.put("mnc", mnc);
                    obj.put("cid", cid);
                    obj.put("lac", lac);
                    obj.put("rssi", signalStrength);
                    list.add(obj);
                } else if (cellInfo instanceof CellInfoWcdma) {
                    CellInfoWcdma cellInfoWcdma = (CellInfoWcdma) cellInfo;
                    CellIdentityWcdma cellIdentityWcdma = cellInfoWcdma.getCellIdentity();
                    CellSignalStrengthWcdma signalStrengthWcdma = cellInfoWcdma.getCellSignalStrength();
                    int mcc = cellIdentityWcdma.getMcc();
                    int mnc = cellIdentityWcdma.getMnc();
                    int cid = cellIdentityWcdma.getCid();
                    int lac = cellIdentityWcdma.getLac();
                    int signalStrength = signalStrengthWcdma.getDbm();
                    // 处理连接的WCDMA基站信息
                    JSONObject obj = new JSONObject();
                    if (i == 0) {
                        obj.put("type", "Connected");
                    } else {
                        obj.put("type", "Neighboring");
                    }
                    obj.put("type1", "WCDMA");
                    obj.put("mcc", mcc);
                    obj.put("mnc", mnc);
                    obj.put("cid", cid);
                    obj.put("lac", lac);
                    obj.put("rssi", signalStrength);
                    list.add(obj);
                } else {
                    Log.w(TAG, "cellInfo类型不支持");
                }
            }
        }
        return list;
    }


    /**
     * 检查授权
     */
    private void checkPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            // Android M Permission check
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (this.mUniSDKInstance.getContext().checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    ((Activity) this.mUniSDKInstance.getContext()).requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_COARSE_LOCATION);
                }
            }
        }
    }

}



